<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="560px" @open="handleDialogOpen()"
    v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="suggestModel" :rules="dialogProps.action === 'add' ? addSuggeset : editSuggeset" ref="suggestForm"
      label-width="80px" label-position="right" size="small">
      <el-tabs type="border-card" v-model="activeTabName">
        <el-tab-pane name="suggestInfoTab" label="基本信息" class="suggest-info-tab-pane">
          <el-form-item label="评估名称" prop="name">
            <el-col :span="16">
              <el-input ref="name" :maxlength="32" v-model="suggestModel.name" autofocus></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="评估结果" prop="consequence">
            <el-col :span="16">
              <el-input ref="consequence" :maxlength="32" v-model="suggestModel.consequence" autofocus></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="组合" prop="combination">
            <el-col :span="16">
              <el-input ref="combination" :maxlength="32" v-model="suggestModel.combination" autofocus></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="建议" prop="advise">
            <el-col :span="16">
              <el-input type="textarea" :maxlength="200" v-model="suggestModel.advise"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="备注" prop="comment">
            <el-col :span="16">
              <el-input type="textarea" :maxlength="32" v-model="suggestModel.comment"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="禁用建议" prop="locked">
            <el-switch v-model="suggestModel.locked" active-color="#ff4949" inactive-color="#13ce66" :active-value="1"
              :inactive-value="0"></el-switch>
          </el-form-item>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('suggestForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { saveSuggest, updateSuggest } from '@/api/Lixueseng520/suggest'

  export default {
    name: "suggest-edit",
    data() {
      return {
        loading: false,
        suggestModel: {
          id: 0,
          name: '',
          consequence: '',
          combination: '',
          advise: '',
          comment: '',
          locked: '',
          roleIds: []
        },
        dialogProps: {
          visible: false,
          action: '',
          title: ''
        },
        activeTabName: 'suggestInfoTab',
        addSuggeset: {
          name: [
            { required: true, message: '请输入评估名称', trigger: 'blur' },
            { min: 2, max: 32, message: '长度在 2 到 32 个字符', trigger: 'blur' }
          ],
          consequence: [
            { required: true, message: '请输入评估结果', trigger: 'blur' },
            { min: 2, max: 32, message: '长度在 2 到 32 个字符', trigger: 'blur' }
          ],
          combination: [
            { required: true, message: '请输入组合', trigger: 'blur' },
            { min: 2, max: 32, message: '长度在 2 到 32 个字符', trigger: 'blur' }
          ],
        },
        editSuggeset: {
          name: [
            { required: true, message: '请输入评估名称', trigger: 'blur' },
            { min: 2, max: 32, message: '长度在 2 到 32 个字符', trigger: 'blur' }
          ],
          consequence: [
            { required: true, message: '请输入评估结果', trigger: 'blur' },
            { min: 2, max: 32, message: '长度在 2 到 32 个字符', trigger: 'blur' }
          ],
          combination: [
            { required: true, message: '请输入组合', trigger: 'blur' },
            { min: 2, max: 32, message: '长度在 2 到 32 个字符', trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            if (this.dialogProps.action === 'add') {
              this.doSaveSuggest()
            } else {
              this.doUpdateSuggest()
            }
          } else {
            return false
          }
        })
      },
      doUpdateSuggest() {
        this.loading = true
        updateSuggest(this.suggestModel.id, this.suggestModel)
          .then(response => {
            this.$message({
              type: 'success',
              message: '修改成功'
            })
            this.loading = false
            this.dialogProps.visible = false
            this.$emit('save-finished')
          })
          .catch(error => {
            this.loading = false
            this.outputError(error)
          })
      },
      doSaveSuggest() {
        this.loading = true
        saveSuggest(this.suggestModel)
          .then(response => {
            this.$message({
              type: 'success',
              message: '添加成功'
            })
            this.loading = false
            this.dialogProps.visible = false
            this.$emit('save-finished')
          })
          .catch(error => {
            this.outputError(error)
          })
      },

      handleDialogClose() {
        this.dialogProps.visible = false
      },
      handleDialogOpen() {
        this.$nextTick(() => {
          this.$refs['suggestForm'].clearValidate()
          this.$refs['name'].focus()
        })
      },
      outputError(error) {
        console.log(error.response ? error.response : error.message)
        this.loading = false
        this.$message({
          showClose: true,
          message: '出错了，请按F12查看浏览器日志。',
          type: 'error'
        })
      }
    },
    mounted: function () {
      this.$nextTick(() => {
        this.$on('openEditUserDialog', function (suggest) {
          this.suggestModel = suggest
          this.dialogProps.action = 'edit'
          this.dialogProps.title = '编辑'
          this.activeTabName = 'suggestInfoTab'
          this.dialogProps.visible = true
        })
        this.$on('openAddUserDialog', function () {
          this.suggestModel = {
            roleIds: []
          }
          this.dialogProps.action = 'add'
          this.dialogProps.title = '新增'
          this.activeTabName = 'suggestInfoTab'
          this.dialogProps.visible = true
        })
      })
    }
  }
</script>

<style lang="scss" scoped>
  .suggest-info-tab-pane {
    margin-top: 10px;
  }
</style>
